package com.bt.lms.ui.page.sys.user;

import com.bt.lms.constants.BOConstant;
import com.bt.lms.entity.User;
import com.bt.lms.entity.page.Page;
import com.bt.lms.ui.page.common.PageLocation;
import com.bt.lms.main.BotLmsApp;
import com.bt.lms.ui.page.sys.utils.MyButton;
import com.bt.lms.ui.page.sys.utils.NetWorkUtils;
import com.bt.lms.ui.vo.PageSkipInfo;
import com.bt.lms.ui.page.sys.user.action.UCSaveBtnAction;
import com.bt.lms.ui.page.sys.utils.MyHBox;
import com.bt.lms.ui.page.sys.utils.MyVBox;
import javafx.collections.ObservableList;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;

/**
 * 编辑用户界面
 */
public class PageCompile extends VBox {

    /**
     * 邮箱输入框id
     */
    public final static String EMAIL_TF = "emailTF";

    /**
     * 邮箱提示id
     */
    public final static String EMAIL_HINT_LAB = "emailMustLab";

    /**
     * 姓名输入框id
     * nameMustLab
     */
    public final static String NAME_TF = "nameTF";

    /**
     * 姓名提示id
     */
    public final static String NAME_HINT_LAB = "nameHintLab";

    /**
     * 证件类型id
     */
    public final static String ID_TYPE_CB = "idTypeCB";

    /**
     * 证件号码id
     */
    public final static String ID_TF = "idTF";

    /**
     * 所属单位id
     */
    public final static String UNIT_TF = "unitTF";

    /**
     * 所属单位提示id
     */
    public final static String UNIT_HINT_LAB = "unitHintTF";

    /**
     * 移动电话id
     */
    public final static String PHONE_TF = "phoneTF";

    /**
     * 移动电话提示id
     */
    public final static String PHONE_HINT_LAB = "phoneHintTF";

    /**
     * 界面跳转信息
     */
    private final PageSkipInfo pageSkipInfo;

    /**
     * 用户
     */
    private final User user;

    /**
     * 构建界面
     *
     * @param user         界面对应的用户
     * @param pageSkipInfo 界面跳转信息
     */
    public PageCompile(User user, PageSkipInfo pageSkipInfo) {

        //维护
        this.user = user;
        this.pageSkipInfo = pageSkipInfo;

        //大小
        int wight = BotLmsApp.PER_WIGHT;
        int height = BotLmsApp.HEIGHT;
        this.setPrefWidth(wight);
        this.setPrefHeight(height);

        //布局
        Insets insets = new Insets(0);
        this.setPadding(insets);

        //组件
        String name = user.getName();
        PageLocation pageLocation = new PageLocation("系统管理", "后台用户管理", "编辑用户" + name);

        //输入区域
        MyVBox inputVBox = new MyVBox();
        inputVBoxInit(inputVBox);

        //控制区域
        MyHBox controlHBox = new MyHBox();
        controlHBoxInit(controlHBox);

//        //关系区域（关联角色）
//        MyHBox userOwnRoleHBox = new MyHBox();
//        userOwnRoleHBoxInit(userOwnRoleHBox);

        //添加组件
        ObservableList<Node> children = this.getChildren();
        children.addAll(pageLocation, inputVBox, controlHBox);
    }

    /**
     * 输入区域初始化
     *
     * @param inputVBox 输入区域
     */
    private void inputVBoxInit(MyVBox inputVBox) {

        //布局
        Insets insets = new Insets(12);
        inputVBox.setPadding(insets);
        inputVBox.setSpacing(8);
        inputVBox.setAlignment(Pos.CENTER_LEFT);

        //组件（说明）
        String nameStr = user.getName();
        String explainStr = nameStr + "的信息";
        Label explainLab = new Label(explainStr);
        //组件添加
        ObservableList<Node> locationChildren = inputVBox.getChildren();
        locationChildren.add(explainLab);

        //组件（位置）
        Label locationLab = new Label("基本信息");
        //组件添加
        ObservableList<Node> explainChildren = inputVBox.getChildren();
        explainChildren.add(locationLab);

        //组件（输入网格
        GridPane gridPane = inputGrid();
        inputVBox.getChildren().add(gridPane);
    }

    /**
     * 网格布局
     *
     * @return 网格布局
     */
    private GridPane inputGrid() {

        //新建网格布局
        GridPane res = new GridPane();
        //大小
        res.setPrefWidth(1280);
        res.setPadding(new Insets(5));
        res.setHgap(15);//间隙（左右）
        res.setVgap(15);//间隙（上下）

        //账号部分 eMail
        Label eMailLab = new Label("账号(电子邮箱)");
        String accountNumberStr = user.getAccountNumber();
        Label accountNumber = new Label(accountNumberStr);
        String text = accountNumber.getText();
        TextField emailTF = new TextField(text);
        emailTF.setId(EMAIL_TF);
        accountNumber.setTextFill(Color.BLACK);
        res.add(eMailLab, 0, 1);
        res.add(emailTF, 1, 1);
        GridPane.setHalignment(eMailLab, HPos.RIGHT);
        Label eMailHintLab = new Label();
        eMailHintLab.setId(EMAIL_HINT_LAB);
        res.add(eMailHintLab, 2, 1);

        //用户姓名部分 name
        Label nameLab = new Label("用户姓名");
        String nameStr = user.getName();
        TextField nameTF = new TextField(nameStr);
        nameTF.setId(NAME_TF);
        res.add(nameLab, 0, 2);
        res.add(nameTF, 1, 2);
        GridPane.setHalignment(nameLab, HPos.RIGHT);
        Label nameHintLab = new Label();
        nameHintLab.setId(NAME_HINT_LAB);
        res.add(nameHintLab, 2, 2);

        //证件类型部分 idType
        Label codeTypeLab = new Label("证件类型");
        ChoiceBox<String> codeTypeCB = new ChoiceBox<>();
        codeTypeCB.setId(ID_TYPE_CB);
        codeTypeCB.getItems().addAll("身份证", "士官证", "学生证", "驾驶证", "护照", "港澳通行证");
        //证件类型匹配
        codeTypeMatch(codeTypeCB);
        res.add(codeTypeLab, 0, 3);
        res.add(codeTypeCB, 1, 3);
        GridPane.setHalignment(codeTypeLab, HPos.RIGHT);

        //证件号码部分 id
        Label codeLab = new Label("证件号码");
        String code = user.getCode();
        TextField idTF = new TextField(code);
        idTF.setId(ID_TF);
        res.add(codeLab, 0, 4);
        res.add(idTF, 1, 4);
        GridPane.setHalignment(codeLab, HPos.RIGHT);

        //所属单位部分 unit
        Label unitLab = new Label("所属单位");
        String unit = user.getUnit();
        TextField unitTF = new TextField(unit);
        unitTF.setId(UNIT_TF);
        res.add(unitLab, 0, 5);
        res.add(unitTF, 1, 5);
        GridPane.setHalignment(unitLab, HPos.RIGHT);
        Label unitHintLab = new Label("*");
        unitHintLab.setTextFill(Color.RED);
        unitHintLab.setId(UNIT_HINT_LAB);
        res.add(unitHintLab, 2, 5);

        //移动电话部分 phone
        Label phoneLab = new Label("移动电话");
        String mobilePhone = user.getMobilePhone();
        TextField phoneTF = new TextField(mobilePhone);
        phoneTF.setId(PHONE_TF);
        res.add(phoneLab, 0, 6);
        res.add(phoneTF, 1, 6);
        GridPane.setHalignment(phoneLab, HPos.RIGHT);
        Label phoneMustLab = new Label("*");
        phoneMustLab.setTextFill(Color.RED);
        res.add(phoneMustLab, 2, 6);
        Label phoneHintLab = new Label();
        phoneHintLab.setId(PHONE_HINT_LAB);
        res.add(phoneHintLab, 2, 2);

        return res;
    }

    /**
     * 控制区域初始化
     *
     * @param controlHBox 控制区域
     */
    private void controlHBoxInit(MyHBox controlHBox) {

        //布局
        Insets insets = new Insets(10);
        controlHBox.setPadding(insets);
        controlHBox.setSpacing(10);
        controlHBox.setAlignment(Pos.CENTER);

        //组件(保存)
        MyButton saveBtn = new MyButton("保存");
        //大小控制
        Insets saveBtnInsets = new Insets(10, 20, 10, 20);
        saveBtn.setPadding(saveBtnInsets);
        //组件添加
        ObservableList<Node> saveBtnChildren = controlHBox.getChildren();
        saveBtnChildren.add(saveBtn);
        //事件
        //当前用户id
        Long id = user.getId();
        UCSaveBtnAction ucSaveBtnAction = new UCSaveBtnAction(pageSkipInfo,id);
        saveBtn.setOnAction(ucSaveBtnAction);

        //组件(取消)
        MyButton cancelBtn = new MyButton("取消");
        //大小控制
        Insets cancelBtnInsets = new Insets(10, 20, 10, 20);
        cancelBtn.setPadding(cancelBtnInsets);
        //事件
        cancelBtn.setOnAction(event -> NetWorkUtils.reqUserPagingPage(pageSkipInfo,new Page<>()));
        //组件添加
        ObservableList<Node> cancelBtnChildren = controlHBox.getChildren();
        cancelBtnChildren.add(cancelBtn);

    }

    /**
     * 证件类型匹配
     * @param codeTypeCB 证件类型
     */
    private void codeTypeMatch(ChoiceBox<String> codeTypeCB) {
        //证件编号
        Integer codeType = user.getCodeType();

        if (codeType.equals(BOConstant.USER_CODE_TYPE_CARD)){
            codeTypeCB.setValue("身份证");

        }else if (codeType.equals(BOConstant.USER_CODE_TYPE_JGZ)){
            codeTypeCB.setValue("士官证");

        }else if (codeType.equals(BOConstant.USER_CODE_TYPE_STUDENT_ID)){
            codeTypeCB.setValue("学生证");

        }else if (codeType.equals(BOConstant.USER_CODE_TYPE_DRIVERS)){
            codeTypeCB.setValue("驾驶证");

        }else if (codeType.equals(BOConstant.USER_CODE_TYPE_PASSPORT)){
            codeTypeCB.setValue("护照");

        }else if (codeType.equals(BOConstant.USER_CODE_TYPE_LAISSEZ_PASSER)){
            codeTypeCB.setValue("港澳通行证");

        }
    }

}
